//
//  StatusViewModel.swift
//  KKWB
//
//  Created by KSY-iOS on 17/1/17.
//  Copyright © 2017年 KK. All rights reserved.
//

import UIKit

class StatusViewModel: NSObject {

    // MARK:- 属性
    var status: StatusModel?
    
    // MARK:- 对数据处理的属性
    var sourceText: String?
    var creatAtText: String?
    var verified_image: UIImage?
    var vip_image: UIImage?
    var profileURL: URL?
    var picURLs : [NSURL] = [NSURL]()   // 处理微博配图的数据
    var cellHeight: CGFloat = 0
    
    
    init(status: StatusModel) {
        super.init()
        
        self.status = status
        
        if let source = status.source, status.source != "" {
            
            let startIndex = (source as NSString).range(of: ">").location + 1
            let length =  (source as NSString).range(of: "</").location - startIndex
            
            sourceText = (source as NSString).substring(with: NSRange(location: startIndex, length: length))
        }
        
        if let created_at = status.created_at {
            
            creatAtText = Date.creatDateString(creatStr: created_at)
        }
        
        if let verified_type = status.user?.verified_type {
            
            switch verified_type {
            case 0:
                verified_image = UIImage(named: "avatar_vip")
            case 2, 3, 5:
                verified_image = UIImage(named: "avatar_enterprise_vip")
            case 220:
                verified_image = UIImage(named: "avatar_grassroot")
            default:
                verified_image = nil
            }
        }
        
        if let mbrank = status.user?.mbrank {
            
            if mbrank > 0, mbrank < 6 {
                
                vip_image = UIImage(named: "common_icon_membership_level\(mbrank)")
            }
        }
        
        if let profileUrlString = status.user?.profile_image_url {
            
            profileURL = URL(string: profileUrlString)
        }
        
        // 6.处理配图数据
        let picURLDicts = status.pic_urls!.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls
        if let picURLDicts = picURLDicts {
            for picURLDict in picURLDicts {
                guard let picURLString = picURLDict["thumbnail_pic"] else {
                    continue
                }
                picURLs.append(NSURL(string: picURLString)!)
            }
        }
    }
    
}
